const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
    certificationArray:["身份证","其他"],
    certificationIndex:0,
    idCard1: [],
    idCard2: [],
    idCard3: [],
    certificationTabIndex:0,
    customItem: '全部',
    date:'',
    qualification: [],
    qualificationId:[],
    realNameVal:'',
    iDCardVal:'',
    enterpriseNameVal: '',
    staffNumVal: '',
    licenseVal: '',
    urlVal: '',
    addressVal: '',
    webUrlVal:'',
    businessLicenseVal:'',
    addressDetailVal:'',
    // 任务信息
    taskid: '',
    tasktitle: '',
    tasksubtitle: '',
    // 地区信息
    id: '',
    subid: '',
    aresname: '',
    aressubname: '',
    authInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.authInfo()
  },

  // r认证信息
  authInfo:function(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'auth/reAuthInfo',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        const {realnameAuth}=res.data.data
        if (res.data.code == 1000) {
          _this.setData({
            realNameVal:realnameAuth.realname,
            iDCardVal:realnameAuth.card_number,
          })
          if(realnameAuth.card_back_side && realnameAuth.card_front_side){
            _this.setData({
              idCard1:[
                {
                  url:realnameAuth.card_front_side,
                  isImage: true,
                }
              ],
              idCard2:[
                {
                  url:realnameAuth.card_back_side,
                  isImage: true,
                }
              ],
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },


  afterRead(){

  },
  // 认证类型
  certificationPickerChange:function(e){
    this.setData({
      certificationIndex: e.detail.value
    })
  },
  // 真实姓名
  realNameInp:function(e){
    this.setData({
      realNameVal: e.detail.value
    })
  },
  // 证件号码
  iDCardInp: function (e) {
    this.setData({
      iDCardVal: e.detail.value
    })
  },


  beforeRead(e){
    const {file}=e.detail
    let _this = this
    const { index } = e.currentTarget.dataset
    if(file.size<3145728){
      wx.showLoading({
        title: '加载中',
      })
      wx.uploadFile({
        url: app.globalData.baseUrl + 'fileUpload',
        filePath: file.url,
        name: 'file',
        formData: {
          token: app.globalData.token,
          file: file.url
        },
        success(upres) {
          wx.hideLoading()
          var obj = JSON.parse(upres.data)
          if (index == 0) {
            _this.setData({
              idCard1: [
                {
                  url:obj.data.url,
                  isImage: true,
                }
              ]
            })
          } else if (index == 1) {
            _this.setData({
              idCard2:  [
                {
                  url:obj.data.url,
                  isImage: true,
                }
              ]
            })
          }
        }
      })
    }else {
      wx.showToast({
        title: '图片不能大于3M',
        icon: 'none',
        duration: 2000
      })
    }
  },


  uploadImg(e){
    let _this = this
    const { file } = e.detail;
    const { index } = e.currentTarget.dataset

  },


  delImg(e){
    const { index } = e.currentTarget.dataset
    if(index==0){
      this.setData({
        idCard1:[]
      })
    }else {
      this.setData({
        idCard2:[]
      })
    }
  },








  // 认证类型
  certificationTab:function(e){
    var { index } = e.currentTarget.dataset
    this.setData({
      certificationTabIndex:index
    })
  },
  // 选择时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 选择行业
  // 选择行业
  industryBtn: function () {
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=0',
    })
  },
  // 选择地址
  areaBtn: function () {
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=1',
    })
  },
  // 删除资质证明
  delQualificationBtn:function(e){
    var { qualification, qualificationId } = this.data
    var { index } = e.currentTarget.dataset
    qualification.splice(index,1)
    qualificationId.splice(index, 1)
    this.setData({
      qualification,
      qualificationId
    })
  },
  // 上传资质证明
  upQualificationImg:function(){
    var _this = this;
    var { qualification, qualificationId } = this.data
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFilePaths[0]
          },
          success(upres) {
            wx.hideLoading()
            var obj = JSON.parse(upres.data)
            const data = upres.data.url
            qualification.push(obj.data.url)
            qualificationId.push(obj.data.id)
            _this.setData({
              qualification: qualification,
              qualificationId: qualificationId
            })
          }
        })
      }
    })
  },
  // 个人认证提交
  personalCertification:function(){
    var { realNameVal, iDCardVal, idCard1, idCard2, idCard3 } = this.data;
    if (realNameVal == ''){
      wx.showToast({
        title: "请输入真实姓名",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (idCard1.length === 0 || idCard2.length ===0){
      wx.showToast({
        title: "请上传您身份证人面像",
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'auth/realnameAuth1',
      data: {
        realname: realNameVal,
        card_number: iDCardVal,
        card_front_side: idCard1[0].url,
        card_back_dside: idCard2[0].url,
        type:1,
        validation_img: idCard3,
        token: app.globalData.token
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          wx.showToast({
            title: "提交成功",
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            })
          },2000)
          wx.setStorageSync('auth_id', res.data.data.auth_id)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 企业名称
  enterpriseNameInp:function(e){
    this.setData({
      enterpriseNameVal:e.detail.value
    })
  },
  // 员工人数
  staffNumInp:function(e){
    this.setData({
      staffNumVal: e.detail.value
    })
  },
  // 营业执照
  licenseInp: function (e) {
    this.setData({
      licenseVal: e.detail.value
    })
  },
  // 企业网址
  urlInp: function (e) {
    this.setData({
      urlVal: e.detail.value
    })
  },
  // 详细地址
  addressInp: function (e) {
    this.setData({
      addressVal: e.detail.value
    })
  },
  // 企业认证
  enterpriseCertificationBtn:function(){
    var url = app.globalData.baseUrl + 'user/enterpriseAuth';
    var { enterpriseNameVal, staffNumVal, licenseVal, urlVal, addressVal, taskid, id, subid, date, qualificationId } = this.data;
    qualificationId = JSON.stringify(qualificationId)
    var data = {
      company_name: enterpriseNameVal,
      cate_id: taskid,
      employee_num: staffNumVal,
      business_license: licenseVal,
      begin_at: date,
      province: id,
      city: subid,
      website:urlVal,
      address: addressVal,
      file_id: qualificationId,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data, (res) => {
      wx.hideLoading()
      console.log(res)
      if (res.code == 1000) {
        wx.showToast({
          title: '认证成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
